We are going to make some plotly plots.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(p8105.datasets)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
let’s get some data.
data("nyc_airbnb")
nyc_airbnb |>
mutate(rating = review_scores_location / 2) |>
select(
borough = neighbourhood_group, neighbourhood, price, room_type,
lat, long, rating) |>
filter(
borough == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500
) |>
drop_na(rating)
## # A tibble: 7,568 × 7
## borough neighbourhood price room_type lat long rating
## <chr> <chr> <dbl> <chr> <dbl> <dbl> <dbl>
## 1 Manhattan Battery Park City 165 Entire home/apt -74.0 40.7 4.5
## 2 Manhattan Battery Park City 225 Entire home/apt -74.0 40.7 5
## 3 Manhattan Battery Park City 299 Entire home/apt -74.0 40.7 5
## 4 Manhattan Battery Park City 168 Entire home/apt -74.0 40.7 5
## 5 Manhattan Battery Park City 100 Entire home/apt -74.0 40.7 5
## 6 Manhattan Battery Park City 225 Entire home/apt -74.0 40.7 5
## 7 Manhattan Battery Park City 250 Entire home/apt -74.0 40.7 5
## 8 Manhattan Battery Park City 110 Entire home/apt -74.0 40.7 4.5
## 9 Manhattan Battery Park City 249 Entire home/apt -74.0 40.7 5
## 10 Manhattan Battery Park City 325 Entire home/apt -74.0 40.7 5
## # … with 7,558 more rows
Lets make a scatterplot !!
nyc_airbnb |>
mutate(
text_label = str_c("Price: ", price)
) |>
plot_ly(
x = ~lat, y = ~long, color = ~ price,
type = "scatter", mode = "markers", alpha = .5, text = ~text_label
)
Can we make boxplots? sure can!
nyc_airbnb |>
mutate(neighbourhood = fct_reorder(neighbourhood, price)) |>
plot_ly(
y = ~price, color = ~neighbourhood,
type = "box", colors = "viridis")
Can we make a bar plot?
nyc_airbnb |>
count(neighbourhood) |>
mutate(neighbourhood = fct_reorder(neighbourhood, n)) |>
plot_ly(x = ~neighbourhood, y = ~n,
type = "bar")
ggp_scatterplot =
nyc_airbnb |>
ggplot(aes(x = lat, y = long, color = price)) +
geom_point()
ggplotly(ggp_scatterplot)